package net.sxnic.workflow.utils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxnic.comm.utils.CommUtils;
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.utils.UgrManagerUtils;
import net.sxnic.workflow.WorkflowConstants;
import net.sxnic.workflow.task.util.TaskUtils;

import org.apache.commons.lang.StringUtils;

/**
 * 传阅处理工具类
 * 
 * @author 孙宇飞
 * 
 */
public class ChuanyueUtils {

	/**
	 * 生成传阅的Html代码
	 * 
	 * @param 节点名称
	 * @return
	 */
	public static String chuanyueHtml(String nodeName) {
		StringBuilder sb = new StringBuilder();
		sb.append("<ul>");

		// // 全部领导
		// sb.append("<li><input type='checkbox' name='chuanyue' value='"
		// + nodeName + TaskUtils.RECEIVES_SEPARATOR
		// + Task.SPECAIL_TASK_ALL_LINGDAO + "'/>" + "所有领导" + "</li>");
		// // 全部处室
		// sb.append("<li><input type='checkbox' name='chuanyue' value='"
		// + nodeName + TaskUtils.RECEIVES_SEPARATOR
		// + Task.SPECAIL_TASK_ALL_CHUSHI + "'/>" + "所有处室" + "</li>");

		// 职能处室
		List<Dept> list = UgrManagerUtils.getDeptManager()
				.findPureFunctionDepart();
		// 领导列表
		List<User> userList = UgrManagerUtils.getUserManager().getAllLeaders();
		for (User uu : userList) {
			sb.append("<li>");
			sb.append("<input type='checkbox' name='chuanyue' value='"
					+ nodeName + TaskUtils.RECEIVES_SEPARATOR
					+ TaskUtils.CHUANYUE_BIAOJI_LINGDAO + uu.getUsername()
					+ "'/>" + UgrConstant.USER_MAP.get(uu.getUsername()) + "");
			sb.append("</li>");
		}
		// 处室列表
		for (Dept g : list) {
			sb.append("<li>");
			sb.append("<input type='checkbox' name='chuanyue' value='"
					+ nodeName + TaskUtils.RECEIVES_SEPARATOR
					+ TaskUtils.CHUANYUE_BIAOJI_CHUSHI + g.getCode() + "'/>"
					+ g.getName() // 处室名
					+ "");
			sb.append("</li>");
		}
		sb.append("</ul>");

		return sb.toString();
	}

	/**
	 * 回显传阅人员
	 * 
	 * @return 厅内传阅:全部处室,gaj,user2,user4
	 */
	public static String showChuanyue(String chuanyue) {
		if (StringUtils.isBlank(chuanyue)) {
			return "";
		}

		String node = StringUtils.substringBefore(chuanyue,
				WorkflowConstants.SPLIT_COLON);
		String cy = StringUtils.substringAfter(chuanyue,
				WorkflowConstants.SPLIT_COLON);
		String[] cys = StringUtils.split(cy, WorkflowConstants.SPLIT_COMMA);

		// 排序-用户名
		Map<String, String> assignee = new LinkedHashMap<String, String>();
		Dept dept = null;
		DeptManager deptManager = UgrManagerUtils.getDeptManager();
		String temp = "";
		for (String c : cys) {

			if (c.startsWith(TaskUtils.CHUANYUE_BIAOJI_LINGDAO)) {
				// 领导
				temp = StringUtils.substringAfter(c,
						TaskUtils.CHUANYUE_BIAOJI_LINGDAO);

				assignee.put(UgrConstant.USER_ORDER_MAP.get(temp),
						UgrConstant.USER_MAP.get(temp));
			} else if (c.startsWith(TaskUtils.CHUANYUE_BIAOJI_CHUSHI)) {
				// 处室 temp 是 处室的代码 如 00 01
				temp = StringUtils.substringAfter(c,
						TaskUtils.CHUANYUE_BIAOJI_CHUSHI);

				dept = deptManager.findByCode(temp);
				assignee.put(String.valueOf(dept.getDeptOrder()),
						dept.getName());
			}
		}

		// 排序
		String[] temp1 = new String[assignee.size()];
		temp1 = CommUtils.SettoArray(assignee.keySet());

		Arrays.sort(temp1);
		String tmp = "";

		for (String ss : temp1) {
			tmp = tmp + assignee.get(ss) + WorkflowConstants.SPLIT_COMMA;
		}

		return node + WorkflowConstants.SPLIT_COLON
				+ StringUtils.removeEnd(tmp, WorkflowConstants.SPLIT_COMMA);
	}

	/**
	 * 根据已选择的传阅处室，回显HTML（已选中的要勾选上）
	 * 2013年之后基本上以此方法为主
	 * @return
	 */
	public static String showChuanyueHtml(String chuanyue) {
		System.out.println("==showChuanyueHtml==="+chuanyue);
		if (StringUtils.isBlank(chuanyue)) {
			return chuanyueHtml("内部传阅");
		}
		
		System.out.println("==showChuanyueHtml==="+chuanyue);

		String nodeName = StringUtils.substringBefore(chuanyue,
				WorkflowConstants.SPLIT_COLON);
		String cy = StringUtils.substringAfter(chuanyue,
				WorkflowConstants.SPLIT_COLON);

		StringBuffer sb = new StringBuffer();
		sb.append("<ul>");
		
		// 职能处室
		List<Dept> list = UgrManagerUtils.getDeptManager()
				.findPureFunctionDepart();
		// 领导列表
		List<User> userList = UgrManagerUtils.getUserManager().getAllLeaders();
		for (User uu : userList) {
			sb.append("<li>");
			sb.append("<input type='checkbox' name='chuanyue' value='"
					+ nodeName + TaskUtils.RECEIVES_SEPARATOR
					+ TaskUtils.CHUANYUE_BIAOJI_LINGDAO + uu.getUsername()
					+ "'" + isSelectedChuanyue(cy, uu.getUsername()) + "/>"
					+ UgrConstant.USER_MAP.get(uu.getUsername()) + "");
			sb.append("</li>");
		}

		// 处室列表
		for (Dept g : list) {
			sb.append("<li>");
			sb.append("<input type='checkbox' name='chuanyue' value='"
					+ nodeName + TaskUtils.RECEIVES_SEPARATOR
					+ TaskUtils.CHUANYUE_BIAOJI_CHUSHI + g.getCode() + "'"
					+ isSelectedChuanyue(cy, g.getCode()) + "/>" + g.getName() // 处室名
					+ "");
			sb.append("</li>");

		}

		sb.append("</ul>");

		return sb.toString();
	}

	/**
	 * 判断是否选中的传阅
	 * 
	 * @param chuanyuers
	 *            已选中的传阅列表
	 * @param assignee
	 *            要判断的用户名
	 * @return
	 */
	public static String isSelectedChuanyue(String chuanyuers, String assignee) {

		if (StringUtils.isBlank(chuanyuers) || StringUtils.isBlank(assignee)) {
			return "";
		}

		if (StringUtils.contains(chuanyuers, assignee)) {
			return " checked='true' ";
		} else
			return "";
	}

	/**
	 * 处理传阅
	 * 
	 * @param chuanyue
	 *            厅内传阅-user1,厅内传阅-user2,厅内传阅-user3
	 * @return 厅内传阅:user1,user2,user3
	 */
	public static String doChuanyue(String chuanyue) {

		if (StringUtils.isBlank(chuanyue)) {
			return "";
		}

		String chuanyueNode = "内部传阅";
		String[] chuanyues = StringUtils.split(chuanyue,
				WorkflowConstants.SPLIT_COMMA);
		String node = "";
		String tmp = "";

		for (String temp : chuanyues) {
			node = StringUtils.substringAfter(temp.trim(), chuanyueNode + "-");

			// 领导
			if (node.startsWith(TaskUtils.CHUANYUE_BIAOJI_LINGDAO)) {
				tmp = tmp + node + WorkflowConstants.SPLIT_COMMA;
			}
			// 处长
			else if (node.startsWith(TaskUtils.CHUANYUE_BIAOJI_CHUSHI)) {
				tmp = tmp + node + WorkflowConstants.SPLIT_COMMA;
			}

		}

		return chuanyueNode + TaskUtils.NODE_NEXTASSIGNEES_SEPARATOR
				+ StringUtils.removeEnd(tmp, WorkflowConstants.SPLIT_COMMA);

	}

	/**
	 * 显示具体的传阅人
	 * 
	 * @param chuanyue
	 *            厅内传阅:user1,user2,user3
	 * @return 厅内传阅:user1Fuallname,user2FullName,user3FullName
	 */
	public String showChuanyueFullNames(String chuanyue) {

		if (StringUtils.isBlank(chuanyue)) {
			return "";
		}

		String nodeName = StringUtils.substringBefore(chuanyue, ":");
		String usernames = StringUtils.substringAfter(chuanyue, ":");

		String[] user_s = StringUtils.split(usernames, ",");

		usernames = "";
		for (String u : user_s) {
			if (StringUtils.isBlank(usernames)) {
				usernames = UgrConstant.USER_MAP.get(u);
			} else {
				usernames = usernames + "," + UgrConstant.USER_MAP.get(u);
			}
		}

		return nodeName + ":" + usernames;
	}

}
